<?php
namespace app\admin\controller;
use think\Request;
use think\Db;
use app\common\model\Dog as DogModel;
use app\common\model\Area as AreaModel;
use app\common\model\DogFamily as DogFamilyModel;
use app\common\model\User as UserModel;
use app\common\model\AdoptSq as AdoptSqModel;
 class Management extends \app\admin\controller\Base{
     protected $res = array(
         'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
         'column'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
         'condition'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
         'apply'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),

     );

     /**
      * @return mixed
      * 首页列表
      */
     public function index(){
         $DogModel = new DogModel();
         $AreaModel = new AreaModel();
         $search = input('get.search/a');
         $page = input('get.page');
         $search = $this->search($search, $page);
         $list = $DogModel->relData(15,$search);
         $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
         $wherePro['level'] = 1;
         if($search[1]!=''){
             $whereCity['pid'] = $search[1];
         }else{
             $whereCity['pid'] = 2;
         }
         if($search[2]!=''){
             $whereDistrict['pid'] = $search[2];

         }else{
             $whereDistrict['pid'] = 52;

         }
         $province =$AreaModel->selectArea($wherePro);
         $city =$AreaModel->selectArea($whereCity);
         $district =$AreaModel->selectArea($whereDistrict);
         $this->assign(
             array(
                 'action'=>$action,
                 'list'=>$list,
                 'page'=>$page,
                 'search' => $search,
                 'province' => $province,
                 'city' => $city,
                 'district' => $district,
                 'dog_adopt_status'=>$DogModel::$dog_adopt_status
             )
         );
         return $this->fetch();
     }

     /**
      * 查询市
      */
     public function city(){
         $city = $_POST['city'];
         $cityArray = Db::table('up_area')->where('pid',$city)->select();
         $data['cityArray'] = $cityArray;
         return json($data);
     }
     /**
      * 查询区
      */
     public function area(){
         $area = $_POST['area'];
         $cityArray = Db::table('up_area')->where('pid',$area)->select();
         $data['cityArray'] = $cityArray;
         return json($data);
     }

     /**
      * @return \think\response\Json
      * excel导出
      */
     public function excelDownload(){
         $search = input('post.search/a');
         $DogModel = new DogModel();
         $DogFamilyModel = new DogFamilyModel();
         $UserModel = new UserModel();
         $res = $DogModel->excelData($search,3,'dog_adopt_status');
         $dog_status = $DogModel::$dog_adopt_status;
         $dog_adopt_environment = $DogModel::$dog_adopt_environment;
         $dog_total = $DogModel::$dog_total;

         $dog_adopt_sterilizations = $DogModel::$dog_adopt_sterilization;

         $udf_sex = $DogFamilyModel::$udf_sex;
         $udf_age =$DogFamilyModel::$udf_age;
         $udf_size =$DogFamilyModel::$udf_size;
         $udf_dog_vaccine =$DogFamilyModel::$udf_dog_vaccine;
         $udf_dog_disability = $DogFamilyModel::$udf_dog_disability;
         $udf_condition = $DogFamilyModel::$udf_condition;

         $sex = $UserModel::$sex;
         $result = array();
         foreach ($res as $key=>$vo){
            $result[$key]['id'] = $vo['dog_id'];//记录ID
            $result[$key]['address'] = $vo['province'].$vo['city'].$vo['district'];//地址
             if($vo['dog_adopt_status']!=''){
                 $result[$key]['dog_status'] = $dog_status[$vo['dog_adopt_status']];//状态
             }else{
                 $result[$key]['dog_status']='';
             }

             $user_time = strtotime($vo['dog_release_time']);
             //发布时间
             $result[$key]['dog_start_time'] =date('Y',$user_time).'/'.date('m',$user_time).'/'.date('d',$user_time).' '.date('H',$user_time).':'.date('i',$user_time);
            //养犬经验
             if($vo['dog_adopt_experience']!=''){
                 $result[$key]['dog_adopt_experience'] =$dog_total[$vo['dog_adopt_experience']]?$dog_total[$vo['dog_adopt_experience']]:'';
             }else{
                 $result[$key]['dog_adopt_experience']='';
             }
            //居住环境
             if($vo['dog_adopt_environment']!=''){
                 $result[$key]['dog_adopt_environment'] = $dog_adopt_environment[$vo['dog_adopt_environment']];
             }else{
                 $result[$key]['dog_adopt_environment']='';
             }
            //绝育
             if($vo['dog_adopt_sterilization']!=''){
                 $result[$key]['dog_adopt_sterilization'] = $dog_adopt_sterilizations[$vo['dog_adopt_sterilization']];

             }else{
                 $result[$key]['dog_adopt_sterilization']='';
             }
             //家访回访
             if($vo['dog_adpot_workers']!=''){
                 $result[$key]['dog_adpot_workers'] = $dog_total[$vo['dog_adpot_workers']];
             }  else{
                 $result[$key]['dog_adpot_workers']='';
             }
            //领养协议
             if($vo['dog_adpot_protocol']!=''){
                 $result[$key]['dog_adpot_protocol'] = $dog_total[$vo['dog_adpot_protocol']];

             }else{
                 $result[$key]['dog_adpot_protocol']='';
             }
             //养犬登记证
             if($vo['dog_adpot_certificate']!=''){
                 $result[$key]['dog_adpot_certificate'] = $dog_total[$vo['dog_adpot_certificate']];
             }else{
                 $result[$key]['dog_adpot_certificate']='';
             }
             //理想领养家庭
             $result[$key]['dog_adpot_information'] = $vo['dog_adpot_information']?$vo['dog_adpot_information']:'';
             //其他说明
             $result[$key]['dog_information'] = $vo['dog_information']?$vo['dog_information']:'';
            //申请领养人数
             $result[$key]['dog_apply_num'] = $vo['dog_apply_num']?$vo['dog_apply_num']:'';
             //狗狗的ID
             $result[$key]['dog_id'] = $vo['dog_id'];
             //狗狗的状态
             if($vo['udf_condition']!=''){
                 $result[$key]['udf_condition'] = $udf_condition[$vo['udf_condition']];
             }else{
                 $result[$key]['udf_condition'] = '';
             }
             //狗狗的性别
             if($vo['udf_sex']!=''){
                 $result[$key]['udf_sex'] = $udf_sex[$vo['udf_sex']];

             }else{
                 $result[$key]['udf_sex'] = '';
             }
             //狗狗的年龄
             if($vo['udf_age']!=''){
                 $result[$key]['udf_age'] = $udf_age[$vo['udf_age']];

             }else{
                 $result[$key]['udf_age'] ='';
             }
             //狗狗的体型
             if($vo['udf_size']!=''){
                 $result[$key]['udf_size'] = $udf_size[$vo['udf_size']];
             }else{
                 $result[$key]['udf_size'] = '';

             }
             //狂犬疫苗
             if($vo['udf_dog_vaccine']!=''){
                 $result[$key]['udf_dog_vaccine'] = $udf_dog_vaccine[$vo['udf_dog_vaccine']];

             }else{
                 $result[$key]['udf_dog_vaccine'] = '';

             }

            //残疾
             if($vo['udf_dog_disability']!=''){
                 $result[$key]['udf_dog_disability'] = $udf_dog_disability[$vo['udf_dog_disability']];

             }else{
                 $result[$key]['udf_dog_disability'] = '';
             }
             //用户ID
             $result[$key]['user_id'] = $vo['user_id'];
             if($vo['user_gender']!=''){
                 $result[$key]['user_sex'] = $sex[$vo['user_gender']];
             }else{
                 $result[$key]['user_sex'] = '';
             }
             if($vo['user_card'] != ''){
                 $result[$key]['user_age'] = $this->getAgeByID($vo['user_card']);
                 $result[$key]['user_address'] = $this->userCard(substr($vo['user_card'],0,2));
             }else{
                 $result[$key]['user_age'] = '';
                 $result[$key]['user_address'] = '';
             }
             //设备
             if($vo['equipment']!=''){
                 $result[$key]['equipment'] =$vo['equipment'];
             }else{
                 $result[$key]['equipment'] ='';
             }

             $result[$key]['total'] = $vo['total'];
         }
         $xlsCell  = array(
             array('id','ID'),
             array('address','城市'),
             array('dog_status','状态'),
             array('dog_start_time','发布时间'),
             array('dog_adopt_experience','养犬经验'),
             array('dog_adopt_environment','居住环境'),
             array('dog_adopt_sterilization','绝育'),
             array('dog_adpot_workers','家访回访'),
             array('dog_adpot_protocol','领养协议'),
             array('dog_adpot_certificate','养犬登记证'),
             array('dog_adpot_information','理想领养家庭'),
             array('dog_information','其他说明'),


             array('dog_apply_num','申请领养人数'),
             array('dog_id','狗狗的ID'),
             array('udf_condition','狗的状况'),
             array('udf_sex','狗的性别'),
             array('udf_age','狗的年龄'),
             array('udf_size','狗的体型'),
             array('udf_dog_vaccine','狂犬疫苗'),
             array('udf_dog_disability','残疾'),
             array('user_id','用户ID'),
             array('user_sex','用户性别'),
             array('user_age','用户年龄'),
             array('user_address','户口所在地'),
             array('equipment','设备'),
             array('total','评论数量'),
         );
         $data = $this->exportExcel($xlsCell,$result);
         return $data;
     }


     /**
      * 领养管理柱状统计页面
      */
     public function column(){
         $AreaModel = new AreaModel();
         $search = input('get.search/a');
         $page = input('get.page');
         $search = $this->search($search, $page);
         $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
         $wherePro['level'] = 1;
         if($search[4]!=''){
             $whereCity['pid'] = $search[4];
         }else{
             $whereCity['pid'] = 2;
         }
         if($search[5]!=''){
             $whereDistrict['pid'] = $search[5];

         }else{
             $whereDistrict['pid'] = 52;
         }
         $province =$AreaModel->selectArea($wherePro);
         $city =$AreaModel->selectArea($whereCity);
         $district =$AreaModel->selectArea($whereDistrict);
         $this->assign(
             array(
                 'action'=>$action,
                 'page'=>$page,
                 'search' => $search,
                 'province' => $province,
                 'city' => $city,
                 'district' => $district,
             )
         );
         return $this->fetch();
     }

     /**
      * @return \think\response\Json
      * 领养申请管理 柱状图
      */
     public function echartsColumn(){
         $DogFamilyModel = new DogFamilyModel();
         $totalDog = $DogFamilyModel->countNum(1);//总的狗狗

         $DogModel = new DogModel();

         $search = input('post.search/a');
         if($search[3]!=''&&$search[4]!=''){
                $date_time = $this->prDates($search[3],$search[4],$search[5]);
         }else{
                $date_time = $this->prDates();
         }

         $count = count($date_time);
         if($search[5]==''){//默认年月日
                $start = $date_time[0];
                $end = $date_time[count($date_time)-1];
                $whereMap['u.dog_time'] = array('between',array($start,$end));
         }else{//证明按了点击按钮
             $whereMap = $this->dateArray($search,$count,$date_time);
         }



         $proposer_status_total = $this->totalDogStatusNum($search,$whereMap,3);//总的领养狗狗申请总和


         $where1['u.dog_adopt_status'] = 2;
//         $proposer_status1 = $DogModel->totalDogNum($search,3,$where1);//待领养数量
         $proposer_status1 = $this->totalDogStatusNum($search,$whereMap,3,$where1);//待领养数量
         if($proposer_status_total!=0){
             $proposer_status1_P =number_format($proposer_status1/$proposer_status_total,2)*100;//待领养的比例

         }else{
             $proposer_status1_P=$proposer_status1*100;
         }
         $where2['u.dog_adopt_status'] = 3;

//         $proposer_status2 = $DogModel->totalDogNum($search,3, $where2);//完成领养数量
         $proposer_status2 = $this->totalDogStatusNum($search,$whereMap,3,$where2);//完成领养数量
         if($proposer_status_total!=0){
             $proposer_status2_P =number_format($proposer_status2/$proposer_status_total,2)*100;//完成领养的比例
         }else{
             $proposer_status2_P = $proposer_status2*100;
         }
         $where3['u.dog_adopt_status'] = 1;

//         $proposer_status3 = $DogModel->totalDogNum($search,3,$where3);//取消领养数量
         $proposer_status3 = $this->totalDogStatusNum($search,$whereMap,3,$where3);//取消领养数量
         if($proposer_status_total!=0){
             $proposer_status3_P =number_format($proposer_status3/$proposer_status_total,2)*100;//取消领养的比例

         }else{
             $proposer_status3_P=$proposer_status3*100;
         }
         $where4['u.dog_adopt_status'] = 4;

//         $proposer_status4 = $DogModel->totalDogNum($search,3,$where4);//重新领养数量
         $proposer_status4 = $this->totalDogStatusNum($search,$whereMap,3,$where3);//重新领养数量
         if($proposer_status_total!=0){
             $proposer_status4_P =number_format($proposer_status4/$proposer_status_total,2)*100;//重新领养的比例
         }else{
             $proposer_status4_P = $proposer_status4*100;
         }
         if($totalDog!=0){
             $total = number_format($proposer_status_total/$totalDog,2)*100;//登记狗狗的占比
         }else{
             $total = $proposer_status_total*100;//登记狗狗的占比
         }
         $res = array(
             'proposer_status1'=>$proposer_status1,
             'proposer_status1_P'=>$proposer_status1_P,
             'proposer_status2'=>$proposer_status2,
             'proposer_status2_P'=>$proposer_status2_P,
             'proposer_status3'=>$proposer_status3,
             'proposer_status3_P'=>$proposer_status3_P,
             'proposer_status4'=>$proposer_status4,
             'proposer_status4_P'=>$proposer_status4_P,
             'total'=>$total
         );
         $result = array();

         foreach ($date_time as $key=>$vo){
             $result['time'][]=$vo;
             if($search[5]==1){
                 $whereHe['u.dog_time'] = array('elt',$vo);
             }else if($search[5]==2){
                 $whereHe['u.dog_month'] = array('elt',$vo);
             }else if($search[5]==3){
                 $whereHe['u.dog_year'] = array('elt',$vo);
             }else{
                 $whereHe['u.dog_time'] = array('elt',$vo);
             }
             if($search[5]==1){
                 $whereHe1['u.dog_time'] = array('EQ',$vo);
             }else if($search[5]==2){
                 $whereHe1['u.dog_month'] = array('EQ',$vo);
             }else if($search[5]==3){
                 $whereHe1['u.dog_year'] = array('EQ',$vo);
             }else{
                 $whereHe1['u.dog_time'] = array('EQ',$vo);
             }
             $value  = $DogModel->columnDog($search,$whereHe);
             $value1  = $DogModel->columnDog($search,$whereHe1);
             $result['value'][]=$value?$value:0;
             $result['value1'][]=$value1?$value1:0;
         }
//         $data  = $DogModel->managementDog($search,'dog_time');//柱状图数据
//         foreach ($data as $key=>$vo){
//             $result['time'][]=$vo['dog_time'];
//             $result['value'][]=$vo['value'];
//         }
         $data = array('totalArray'=>$result,'res'=>$res,'status'=>200);
         return json($data);
     }

     /**
      * @param $search
      * @param $whereMap
      * @param string $status
      * @return int|string
      * 统计发布狗狗动态的数量
      */
     public function totalDogStatusNum($search,$whereMap,$type,$whereH=1){
         $where = '';
         if($search[0]!=''){
             $where['u.area_sheng'] = array('=',$search[0]);
         }
         if($search[1]!=''){
             $where['u.area_shi'] = array('=',$search[1]);
         }
         if($search[2]!=''){
             $where['u.area_qu'] = array('=',$search[2]);
         }
         $where['u.dog_type'] = $type;
         $branch = Db::table('up_dog')->alias('u')
//             ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
//             ->join('up_area ua2','ua2.district_id = u.area_shi','left')
//             ->join('up_area ua3','ua3.district_id = u.area_qu','left')
             ->where($where)
             ->where($whereMap)
             ->where($whereH)
             ->count();
         //总的数据
         return $branch;
     }


     /**
      * @return mixed
      * 领养条件情况
      */
     public function condition(){
         $AreaModel = new AreaModel();
         $search = input('get.search/a');
         $page = input('get.page');
         $search = $this->search($search, $page);
         $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
         $wherePro['level'] = 1;
         if($search[4]!=''){
             $whereCity['pid'] = $search[4];
         }else{
             $whereCity['pid'] = 2;
         }
         if($search[5]!=''){
             $whereDistrict['pid'] = $search[5];

         }else{
             $whereDistrict['pid'] = 52;
         }
         $province =$AreaModel->selectArea($wherePro);
         $city =$AreaModel->selectArea($whereCity);
         $district =$AreaModel->selectArea($whereDistrict);
         $this->assign(
             array(
                 'action'=>$action,
                 'page'=>$page,
                 'search' => $search,
                 'province' => $province,
                 'city' => $city,
                 'district' => $district,
             )
         );
         return $this->fetch();
     }

     /***
      * @return \think\response\Json
      * 领养条件echarts图
      */
     public function conditionEcharts(){
         $DogModel = new DogModel();
         $search = input('post.search/a');
         $total =$DogModel->branchCount($search);
         $dog_adopt_experience  = $DogModel->branch($search,'','dog_adopt_experience');//养狗经验
         $dog_adopt_environment  = $DogModel->branch($search,'','dog_adopt_environment');//居住环境
         $dog_adopt_sterilization  = $DogModel->branch($search,'','dog_adopt_sterilization');//绝育
         $dog_adpot_workers  = $DogModel->branch($search,'','dog_adpot_workers');//家访回访
         $dog_adpot_protocol  = $DogModel->branch($search,'','dog_adpot_protocol');//领养协议
         $dog_adpot_certificate  = $DogModel->branch($search,'','dog_adpot_certificate');//办理养犬登记证
         $totalArray = array();
         $totalNum = array();
         $totalName = array();
         foreach ($dog_adopt_experience as $key=>$vo){
             if($vo['dog_adopt_experience']==1){
                 $vo['name'] = '需要';
             }elseif($vo['dog_adopt_experience']==2){
                 $vo['name'] = '不需要';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adopt_experience']);
             $totalArray['dog_adopt_experience'][] = $vo['name'];
             $totalNum['dog_adopt_experience'][] = $vo['num'];
             $totalName['dog_adopt_experience']='养狗经验';
         }

         foreach ($dog_adopt_environment as $key=>$vo){
             if($vo['dog_adopt_environment']==1){
                 $vo['name'] = '不接受合租';
             }elseif($vo['dog_adopt_environment']==2){
                 $vo['name'] = '需要郊区';
             }elseif($vo['dog_adopt_environment']==3){
                 $vo['name'] = '无要求';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adopt_environment']);

             $totalArray['dog_adopt_environment'][] = $vo['name'];
             $totalNum['dog_adopt_environment'][] = $vo['num'];
             $totalName['dog_adopt_environment']='居住环境';
         }

         foreach ($dog_adopt_sterilization as $key=>$vo){
             if($vo['dog_adopt_sterilization']==1){
                 $vo['name'] = '未绝育';
             }elseif($vo['dog_adopt_sterilization']==2){
                 $vo['name'] = '已绝育';
             }elseif($vo['dog_adopt_sterilization']==3){
                 $vo['name'] = '无要求';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adopt_sterilization']);
             $totalArray['dog_adopt_sterilization'][] = $vo['name'];
             $totalNum['dog_adopt_sterilization'][] = $vo['num'];
             $totalName['dog_adopt_sterilization']='绝育';
         }

         foreach ($dog_adpot_workers as $key=>$vo){
             if($vo['dog_adpot_workers']==1){
                 $vo['name'] = '需要';
             }elseif($vo['dog_adpot_workers']==2){
                 $vo['name'] = '不需要';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adpot_workers']);
             $totalArray['dog_adpot_workers'][] = $vo['name'];
             $totalNum['dog_adpot_workers'][] = $vo['num'];
             $totalName['dog_adpot_workers']='家访回访';
         }

         foreach ($dog_adpot_protocol as $key=>$vo){
             if($vo['dog_adpot_protocol']==1){
                 $vo['name'] = '需要';
             }elseif($vo['dog_adpot_protocol']==2){
                 $vo['name'] = '不需要';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adpot_protocol']);
             $totalArray['dog_adpot_protocol'][] = $vo['name'];
             $totalNum['dog_adpot_protocol'][] = $vo['num'];
             $totalName['dog_adpot_protocol']='领养协议';
         }

         foreach ($dog_adpot_certificate as $key=>$vo){
             if($vo['dog_adpot_certificate']==1){
                 $vo['name'] = '需要';
             }elseif($vo['dog_adpot_certificate']==2){
                 $vo['name'] = '不需要';
             }
             if($total!=0){
                 $vo['num']=number_format($vo['value']/$total,2)*100;
             }else{
                 $vo['num'] = $vo['value'];
             }
             unset($vo['dog_adpot_certificate']);
             $totalArray['dog_adpot_certificate'][] = $vo['name'];
             $totalNum['dog_adpot_certificate'][] = $vo['num'];
             $totalName['dog_adpot_certificate']='办理养犬登记证';
         }
         $data = array('totalArray'=>$totalArray,'totalNum'=>$totalNum,'status'=>200,'totalName'=>$totalName);
         return json($data);
     }

     /**
      * @return mixed
      * 领取申请页面
      */
     public function apply(){
         $AreaModel = new AreaModel();
         $search = input('get.search/a');
         $page = input('get.page');
         $search = $this->search($search, $page);
         $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
         $wherePro['level'] = 1;
         if($search[4]!=''){
             $whereCity['pid'] = $search[4];
         }else{
             $whereCity['pid'] = 2;
         }
         if($search[5]!=''){
             $whereDistrict['pid'] = $search[5];

         }else{
             $whereDistrict['pid'] = 52;
         }
         $province =$AreaModel->selectArea($wherePro);
         $city =$AreaModel->selectArea($whereCity);
         $district =$AreaModel->selectArea($whereDistrict);
         $this->assign(
             array(
                 'action'=>$action,
                 'page'=>$page,
                 'search' => $search,
                 'province' => $province,
                 'city' => $city,
                 'district' => $district,
             )
         );
         return $this->fetch();
     }

     /**
      * @return \think\response\Json
      * 领养申请条件图表
      */
     public function applyEcharts(){
         $totalAge=$user=$proposer=$experience=$dataRes=$dataH=$petyet=$dataReason=$dataCohabit=$dataCare=array();
         $AdoptSqModel = new AdoptSqModel();
         $search = input('post.search/a');
         $ageCard = $AdoptSqModel->userAge($search,'user_card');//年龄
         $sex = $AdoptSqModel->userSex($search);//性别
         $proposer_long_time = $AdoptSqModel->totalData($search,'proposer_long_time');//居住时间
         $proposer_long_time_total = $AdoptSqModel->total($search);//居住时间总数
         $proposer_experience = $AdoptSqModel->totalData($search,'proposer_experience');//养狗经验
         $proposer_experience_total = $AdoptSqModel->total($search);//养狗经验总数

         $proposer_petyet = $AdoptSqModel->totalData($search,'proposer_petyet');//家中是否有宠物
         $proposer_petyet_total = $AdoptSqModel->total($search);//家中是否有宠物总数

         //年龄
         $count = count($ageCard);
         if($count>0){
             $start = $ageCard[$count-$count];
             for ($i=$start;$i<=$ageCard[$count-1];$i++){
                 $year = date('Y');
                 $Differ = $year-$i;
                 $where['user_card'] = array('like',"%$Differ%");
                 $total = Db::table('up_u_user')->where($where)->count();
                 $totalAge['age'][]= $i;
                 $totalAge['total'][]= $total;
             }
         }

         //性别
         foreach ($sex['branch'] as $key=>$vo){
            if($vo['user_gender']==2){
                $Proportion = number_format($vo['value']/$sex['count'],2)*100;
                $vo['name']= $Proportion.'%'.' ;'.$vo['value'].'  '.':女';
//                $vo['name']= '女';
            }else{
                $Proportion = number_format($vo['value']/$sex['count'],2)*100;
                $vo['name']= '男'.': '.$vo['value'].';'.' '.$Proportion.'%';

            }
            unset($vo['user_gender']);
            $user['sex'][]= $vo;
         }

         //本市居住时间
         foreach ($proposer_long_time as $kk=>$vv){
                if($vv['proposer_long_time']==1){
                    $vv['name']='0-12个月';
                }elseif ($vv['proposer_long_time']==2){
                    $vv['name']='1-3年';
                }elseif ($vv['proposer_long_time']==3){
                    $vv['name']='4-6年';
                }else{
                    $vv['name'] = '7年以上';
                }
                $proposer['name'][]=$vv['name'];
             if($proposer_long_time_total!=0){
                 $proposer['num'][]=number_format($vv['value']/$proposer_long_time_total,2)*100;
             }else{
                 $proposer['num'][] = $vv['value'];
             }
         }
         //养狗经验
         foreach ($proposer_experience as $kk1=>$vv1){
             if($vv1['proposer_experience']==1){
                 $vv1['name']='无';
             }elseif ($vv1['proposer_experience']==2){
                 $vv1['name']='少于1年';
             }elseif ($vv1['proposer_experience']==3){
                 $vv1['name']='1-3年';
             }else{
                 $vv1['name'] = '3年以上';
             }
             $experience['name'][]=$vv1['name'];

             if($proposer_experience_total!=0){
                 $experience['num'][]=number_format($vv1['value']/$proposer_experience_total,4)*100;
             }else{
                 $experience['num'][]=$vv1['value'];
             }
         }
         //居住环境
         $environment1 = array('environment_id'=>0,'environment_name'=>'其他');
         $environment = Db::table('up_proposer_environment')->select();
         $environment_total = $AdoptSqModel->total($search);//养居住环境总数

         array_unshift($environment,$environment1);
         foreach ($environment as $he=>$eo){
             $whereE['u.proposer_environment'] = $eo['environment_id'];
             $data = $AdoptSqModel->forTotal($whereE,$search);
             $dataRes['name'][]=$eo['environment_name'];
//             $dataRes['num'][]=$data;
             if($environment_total!=0){
                 $dataRes['num'][]=number_format($data/$environment_total,2)*100;
             }else{
                 $dataRes['num'][] = $data;
             }
         }
         //房屋
         $house1 = array('house_id'=>0,'house_name'=>'其他');
         $house = Db::table('up_proposer_house')->select();
         $house_total = $AdoptSqModel->total($search);//养居住环境总数

         array_unshift($house,$house1);
         foreach ($house as $hy=>$ho){
             $whereH['u.proposer_house'] = $ho['house_id'];
             $data1 = $AdoptSqModel->forTotal($whereH,$search);
             if($house_total!=0){
                 $dataH['num'][]=number_format($data1/$house_total,2)*100;
             }else{
                 $dataH['num'][]=$data1;
             }
             $dataH['name'][]=$ho['house_name'];
//             $dataH['num'][]=$data1;
         }

         //家中是否有宠物
         foreach ($proposer_petyet as $kk2=>$vv2){
             if($vv2['proposer_petyet']==1){
                 $vv2['name']='有';
             }else{
                 $vv2['name']='没有';
             }
             $petyet['name'][]=$vv2['name'];
//             $petyet['num'][]=$vv2['value'];

             if($proposer_petyet_total!=0){
                 $petyet['num'][]=number_format($vv2['value']/$proposer_petyet_total,2)*100;
             }else{
                 $petyet['num'][]=$vv2['value'];
             }
         }

         //领养原因
         $reason1 = array('reason_id'=>0,'reason_name'=>'其他');
         $reason = Db::table('up_proposer_reason')->select();
         $reason_total = $AdoptSqModel->total($search);//养居住环境总数

         array_unshift($reason,$reason1);
         foreach ($reason as $hh=>$ho1){
             $reasonWhere = ','.$ho1['reason_id'].',';
             $whereH1['u.proposer_reason'] = array('like',"%$reasonWhere%");
             $data2 = $AdoptSqModel->forTotal($whereH1,$search);
             if($house_total!=0){
                 $dataReason['num'][]=number_format($data2/$reason_total,2)*100;
             }else{
                 $dataReason['num'][]=$data2;
             }
             $dataReason['name'][]=$ho1['reason_name'];
//             $dataReason['num'][]=$data2;
         }
         //同住人员
         $cohabit1 = array('cohabit_id'=>0,'cohabit_name'=>'其他');
         $cohabit = Db::table('up_proposer_cohabit')->select();
         $cohabit_total = $AdoptSqModel->total($search);

         array_unshift($cohabit,$cohabit1);
         foreach ($cohabit as $hc=>$hcv){
             $cohabitWhere = ','.$hcv['cohabit_id'].',';
             $whereH3['u.proposer_cohabit'] = array('like',"%$cohabitWhere%");
             $data3 = $AdoptSqModel->forTotal($whereH3,$search);
             $dataCohabit['name'][]=$hcv['cohabit_name'];
//             $dataCohabit['num'][]=$data3;
             if($cohabit_total!=0){
                 $dataCohabit['num'][]=number_format($data3/$cohabit_total,2)*100;
             }else{
                 $dataCohabit['num'][]=$data3;
             }
         }

         //领养后照顾人
         $care1 = array('care_id'=>0,'care_name'=>'其他');
         $care = Db::table('up_proposer_care')->select();
         $care_total = $AdoptSqModel->total($search);

         array_unshift($care,$care1);
         foreach ($care as $hc=>$hcv){
             $careWhere = ','.$hcv['care_id'].',';
             $whereH4['u.proposer_care'] = array('like',"%$careWhere%");
             $data4 = $AdoptSqModel->forTotal($whereH4,$search);
             $dataCare['name'][]=$hcv['care_name'];
             if($care_total!=0){
                 $dataCare['num'][]=number_format($data4/$care_total,2)*100;
             }else{
                 $dataCare['num'][]=$data4;
             }
//             $dataCare['num'][]=$data4;
         }
         return json(
            array(
            'totalAge'=>$totalAge,
            'userSex'=>$user,
            'proposer'=>$proposer,
            'experience'=>$experience,
            'environment'=>$dataRes,
            'house'=>$dataH,
            'petyet'=>$petyet,
            'dataReason'=>$dataReason,
            'dataCohabit'=>$dataCohabit,
            'dataCare'=>$dataCare
            )
        );
     }

     /**
      * @return mixed|\think\response\Json
      * 详情
      */
     public function detail(){
         $id = input('get.id');
         $where['dog_id'] = $id;
         $res = Db::table('up_dog')->where($where)->find();
         $dog = Db::table('up_dog_family')->where('udf_id',$res['udf_id'])->find();
         if($res['dog_type']!=3){
             return $this->api_dbError("不能查看其他狗的详情");
         }
         if($res['dog_adopt_experience']==1){//是否养犬经验
            $res['experience'] ='需要';
         }else{
             $res['experience'] ='不需要';
         }

         if($res['dog_adopt_environment']==1){//居住环境
             $res['environment'] ='不接受合租';
         }else if($res['dog_adopt_environment']==2){
             $res['environment'] ='需要郊区';
         }else{
             $res['environment'] ='无要求';
         }

         if($res['dog_adopt_sterilization']==1){//绝育
             $res['sterilization'] ='未绝育';
         }else if($res['dog_adopt_sterilization']==2){
             $res['sterilization'] ='已绝育';
         }else{
             $res['sterilization'] ='无要求';
         }

         if($res['dog_adpot_workers']==1){//家访
             $res['workers'] ='需要';
         }else{
             $res['workers'] ='不需要';
         }

         if($res['dog_adpot_protocol']==1){//领养协议
             $res['protocol'] ='需要';
         }else{
             $res['protocol'] ='不需要';
         }

         if($res['dog_adpot_certificate']==1){//办理养犬登记证
             $res['certificate'] ='需要';
         }else{
             $res['certificate'] ='不需要';
         }

         if($dog['udf_sex']==1){//狗狗的性别
             $res['sex'] ='公';
         }else{
             $res['sex'] ='母';
         }
         if($dog['udf_size']==1){//狗狗的体型
             $res['size'] ='小型犬';
         }else if($dog['udf_size']==2){
             $res['size'] ='中型犬';
         }else{
             $res['size'] ='大型犬';
         }
         $this->assign(array(
             'res'=>$res
         ));
         return $this->fetch();
     }

     /***
      * @return \think\response\Json
      * 删除
      */
     public function delDog(){
         $ids = strtoarray(input('post.ids'));
         $DogFamilyModel = new DogModel();
         $where['dog_id'] = array('In', $ids);
         $result = $DogFamilyModel->where($where)->delete();
         if ($result) {
             return $this->api_success('操作成功');
         } else {
             return $this->api_dbError();
         }
     }
 }